import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [{
    path: '/login',
    component: () => import('@/components/MyLogin')
  },
  {
    path: '/home',
    component: () => import('@/components/MyHome'),
    children: [{
        path: 'goods',
        component: () => import('@/components/menus/MyGoods')
      },
      {
        path: 'orders',
        component: () => import('@/components/menus/MyOrders')
      },
      {
        path: 'rights',
        component: () => import('@/components/menus/MyRights')
      },
      {
        path: 'settings',
        component: () => import('@/components/menus/MySettings')
      },
      {
        path: 'users',
        component: () => import('@/components/menus/MyUsers')
      },
      {
        path: 'detail/:id',
        component: () => import('@/components/user/MyUserDetail')
      }
    ]
  }
]


const router = new VueRouter({
  routes
})
router.beforeEach((to,from,next)=>{
 if (to.path === '/login') {
   next()
 }else{
  if (localStorage.getItem('token')) {
    next()
  }else
    next('/login')
 }
})


export default router